package com.patui.Weibo;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

import android.util.Log;
public class RequestTokenHeader extends HttpHeaderFactory {
	public final static String TAG="@___Patui.RequestTokenHeader";
	public final static String SPLITTER="___";

	//算法写的太垃圾了,这里以后必须优化.
	public WeiboParameters generateSignatureSort(WeiboParameters bundle)
	{
		//Log.i(TAG,SPLITTER+"generateSignature generateSignatureSort start:");
		if(bundle == null || (bundle.size() == 0)){
			return null;
		}
		WeiboParameters pp = new WeiboParameters();
		String key = "oauth_callback";
		pp.add(key, bundle.getValue(key));
		key = "oauth_consumer_key";
		pp.add(key, bundle.getValue(key));
		key = "oauth_nonce";
		pp.add(key, bundle.getValue(key));
		key = "oauth_signature_method";
		pp.add(key, bundle.getValue(key));
		key = "oauth_timestamp";
		pp.add(key, bundle.getValue(key));
		key = "oauth_version";
		pp.add(key, bundle.getValue(key));
		key = "source";
		pp.add(key, bundle.getValue(key));
		return pp;
	}
	
	//生成oauth_signature
	public String generateSignature(String signatureStr, Token token) throws WeiboException {
		//Log.i(TAG,SPLITTER+"generateSignature getRequest_toke start:");
		byte[] byteHMAC = null;
        try {
            Mac mac = Mac.getInstance(HttpHeaderFactory.CONST_HMAC_SHA1);
            SecretKeySpec spec = null;
            String oauthKey = encode(Weibo.APP_SECRET) + "&";
            spec = new SecretKeySpec(oauthKey.getBytes(), HttpHeaderFactory.CONST_HMAC_SHA1);
            mac.init(spec);
            byteHMAC = mac.doFinal(signatureStr.getBytes());
        } catch (InvalidKeyException e) {
        	throw new WeiboException(e);
        } catch (NoSuchAlgorithmException e) {
        	throw new WeiboException(e);
        }
        return String.valueOf(Utility.base64Encode(byteHMAC));
	}

	@Override
	public void addAdditionalParams(WeiboParameters des, WeiboParameters src) {
		// TODO Auto-generated method stub
		
	}

}
